package com.example.hms.service.servicelmpl;

import com.example.hms.dao.DashboardDao;
import com.example.hms.entity.Dashboard;
import com.example.hms.service.DashboardService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DashboardServiceImpl implements DashboardService {
    @Resource
    private DashboardDao dashboardDao;

    @Override
    public Result selectRoomNumber() {
        return ResultSubmit.success(dashboardDao.selectRoomNumber());
    }

    @Override
    public Result selectLiveNumber() {
        return  ResultSubmit.success(dashboardDao.selectLiveNumber());
    }

    public Result selectRevenue(String month){
        // 前端传递数据类型为：yyy-MM(年-月)
        //数据库需要的数据类型为:YYY-MM-dd hh:mm:ss(年-月-日 时-分-秒)
        month = month + "-01 00:00:00";
        return  ResultSubmit.success(dashboardDao.selectRevenue(month));
    }

    @Override
    public Result selectRevenueByMonth(String month) {
        List<Dashboard> 多月查询数据汇总 = new ArrayList<>();
        for (int i=0;i<6;i++){
            Dashboard 单月收入信息 = new Dashboard();
            单月收入信息.setMonth(month);
            单月收入信息.setMoney(dashboardDao.selectRevenue(month+"-01 00:00:00"));
            多月查询数据汇总.add(单月收入信息);
            // 月份处理
             month = monthUpdate1(month);
        }
        return  ResultSubmit.success(多月查询数据汇总);
    }
    private  String monthUpdate1(String month){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = null;
        try{
            date = format.parse(month);
        }catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,-1);
        return format.format(calendar.getTime());
    }

    private String monthUpdate2(String month){
    // month的数据格式 YYYY-MM(年 - 月)
    StringBuffer stringBuffer = new   StringBuffer(month);
    int year = Integer.parseInt(stringBuffer.substring(0,4));
    int month1 = Integer.parseInt(stringBuffer.substring(5,7));

    // 实现月份减1
    if (month1 == 1){
        year = year -1;
        month1 = 12;
    }else {
        month1 = month1-1;
        }
    if (month1 < 10){
        return  year + "-0" + month;
    }
    return year + "-" + month1;
    }
}
